﻿angular.module('app.masterdata.customer', [])
    .config(['$routeProvider', function ($routeProvider) {
        $routeProvider
        .when('/admin/customer', {
            templateUrl: 'app/masterdata/customer/customer-list.tpl.html',

            controller: 'customerListCtrl',
            resolve: {
                //首次加载第一页
                statuslist: ['baseDataProxy', function (baseDataProxy) {
                    return baseDataProxy.SysTypeDetail("005");
                }],
                custPaging: ['customerProxy', function (customerProxy) {
                    var now = new Date().format("yyyy-MM-dd");                  
                    return customerProxy.customerPaging(1, 20, "&$filter=(CreateTime ge " + now + ")");
                }],
                internallist: ['baseDataProxy', function (baseDataProxy) {
                    return baseDataProxy.SysTypeDetail("024");
                }]
            }
        });
    }])

//*****************************************header***************************s
    .controller('customerListCtrl',
      ['$scope', '$interval', 'customerProxy', 'modalService', 'custPaging', 'statuslist', 'internallist',
       function ($scope, $interval, customerProxy, modalService, custPaging, statuslist, internallist) {

           $scope.statuslist = statuslist;
           $scope.internallist = internallist;
           var now = new Date();
           $scope.startTime=now.format("yyyy-MM-dd");
           $scope.customerList = {
               multiSelect: false,
               enableFullRowSelection: true,
               noUnselect: true,
               columnDefs: [
                    { field: 'customerNum', displayName: 'Customer Code', width: '120' },
                    { field: 'customerName', displayName: 'Customer Name', width: '170' },
                    { field: 'collector', displayName: 'Collector', width: '100' },
                    { field: 'collectorName', displayName: 'Collector Name', width: '120' },
                    { field: 'billGroupCode', displayName: 'Bill Group Code', width: '120' },
                    { field: 'billGroupName', displayName: 'Factory Group Name', width: '200' },
                    { field: 'isIc', displayName: 'IS IC', width: '100' },
                    { field: 'class', displayName: 'Customer Class', width: '115' },
                    { field: 'country', displayName: 'Country', width: '120' },
                    { field: 'flgName', displayName: 'Account Status', width: '110'}                  
               ],
               onRegisterApi: function (gridApi) {
                   //set gridApi on scope
                   $scope.gridApi = gridApi;
               }
           };

           $scope.searchCustomer = function () {

               //组合过滤条件
               var filterStr = '';
               if ($scope.custCode) {
                   if (filterStr != "") {
                       filterStr += "and (contains(CustomerNum,'" + $scope.custCode + "'))";
                   } else {
                       filterStr += "&$filter=(contains(CustomerNum,'" + $scope.custCode + "'))";
                   }
               }
               if ($scope.custName) {
                   if (filterStr != "") {
                       filterStr += "and (contains(CustomerName,'" + $scope.custName + "'))"
                   } else {
                       filterStr += "&$filter=(contains(CustomerName,'" + $scope.custName + "'))";
                   }
               }
               if ($scope.status) {
                   if (filterStr != "") {
                       filterStr += "and (IsHoldFlg eq '" + $scope.status + "')";
                   } else {
                       filterStr += "&$filter=(IsHoldFlg eq '" + $scope.status + "')";
                   }
               }
               if ($scope.groupName) {
                   if (filterStr != "") {
                       filterStr += "and (contains(BillGroupName,'" + $scope.groupName + "'))"
                   } else {
                       filterStr += "&$filter=(contains(BillGroupName,'" + $scope.groupName + "'))";
                   }
               }
               if ($scope.collector) {
                   if (filterStr != "") {
                       filterStr += "and (contains(Collector,'" + $scope.collector + "'))"
                   } else {
                       filterStr += "&$filter=(contains(Collector,'" + $scope.collector + "'))";
                   }
               }
               if ($scope.isic) {
                   if (filterStr != "") {
                       filterStr += "and (ExcludeFlg eq '" + $scope.isic + "')";
                   } else {
                       filterStr += "&$filter=(ExcludeFlg eq '" + $scope.isic + "')";
                   }
               }
               if ($scope.misCollect) {
                   if (filterStr != "") {
                       filterStr += "and (Collector eq null)";
                   } else {
                       filterStr += "&$filter=(Collector eq null)";
                   }
               }
               if ($scope.misGroup) {
                   if (filterStr != "") {
                       filterStr += "and (BillGroupName eq null)";
                   } else {
                       filterStr += "&$filter=(BillGroupName eq null)";
                   }
               }
               if ($scope.startTime) {
                   if (filterStr != "") {
                       filterStr += "and (CreateTime ge " + $scope.startTime + ")";
                   } else {
                       filterStr += "&$filter=(CreateTime ge " + $scope.startTime + ")";
                   }
               }
               if ($scope.endTime) {
                   if (filterStr != "") {
                       filterStr += "and (CreateTime le " + $scope.endTime + ")";
                   } else {
                       filterStr += "&$filter=(CreateTime le " + $scope.endTime + ")";
                   }
               }     
               if ($scope.billCode) {
                   if (filterStr != "") {
                       filterStr += "and (contains(BillGroupCode,'" + $scope.billCode + "'))"
                   } else {
                       filterStr += "&$filter=(contains(BillGroupCode,'" + $scope.billCode + "'))";
                   }
               }
               if ($scope.country) {
                   if (filterStr != "") {
                       filterStr += "and (contains(Country,'" + $scope.country + "'))"
                   } else {
                       filterStr += "&$filter=(contains(Country,'" + $scope.country + "'))";
                   }
               }
               filstr = filterStr;
               //    $scope.currentPage = 1;
               customerProxy.customerPaging($scope.currentPage, $scope.itemsperpage, filstr, function (list) {
                   if (list != null) {
                       $scope.totalItems = list[0].count;
                       $scope.customerList.data = list[0].results;
                       $interval(function () { $scope.gridApi.selection.selectRow($scope.customerList.data[0]); }, 0, 1);
                   }
               })
           };

           if (custPaging) {
               $scope.customerList.data = custPaging[0].results;
               $scope.totalItems = custPaging[0].count; //查询结果初始化
               $interval(function () { $scope.gridApi.selection.selectRow($scope.customerList.data[0]); }, 0, 1);
           }

           $scope.selectedLevel = 20;  //下拉单页容量初始化
           $scope.itemsperpage = 20;
           $scope.currentPage = 1; //当前页
           $scope.maxSize = 10; //分页显示的最大页     
           var filstr = "&$filter=(CreateTime ge " + now.format("yyyy-MM-dd") + ")";
           //分页容量下拉列表定义
           $scope.levelList = [
                    { "id": 20, "levelName": '20' },
                    { "id": 500, "levelName": '500' },
                    { "id": 1000, "levelName": '1000' },
                    { "id": 2000, "levelName": '2000' },
                    { "id": 5000, "levelName": '5000' }
           ];
           //单页容量变化
           $scope.pagesizechange = function (selectedLevelId) {
               var index = $scope.currentPage;
               customerProxy.customerPaging(index, selectedLevelId, filstr, function (list) {
                   $scope.itemsperpage = selectedLevelId;
                   $scope.customerList.data = list[0].results;
                   $scope.totalItems = list[0].count;
                   $interval(function () { $scope.gridApi.selection.selectRow($scope.customerList.data[0]); }, 0, 1);
               });
           };

           //翻页
           $scope.pageChanged = function () {
               //alert("d");
               var index = $scope.currentPage;
               customerProxy.customerPaging(index, $scope.itemsperpage, filstr, function (list) {
                   $scope.customerList.data = list[0].results;
                   $interval(function () { $scope.gridApi.selection.selectRow($scope.customerList.data[0]); }, 0, 1);
                   $scope.totalItems = list[0].count;
               }, function (error) {
                   alert(error);
               });
           };

           $scope.resetSearch = function () {
               filstr = "";
               $scope.custCode = "";
               $scope.custName = "";
               $scope.status = "";
               $scope.groupName="";
               $scope.collector="";
               $scope.isic="";
               $scope.startTime="";
               $scope.endTime="";
               $scope.misCollect="";
               $scope.misGroup="";
               $scope.billCode="";
               $scope.country="";
           }

           $scope.editCustomer = function () {
               if ($scope.gridApi.selection.getSelectedRows()) {
                   var strids = [];
                   var row = $scope.gridApi.selection.getSelectedRows()[0];
                   strids.push(row.customerNum);
                   window.open('#/cust/masterData/' + strids);
               }
           }

           $scope.addCustomer = function () {
               var strids = "newCust";
               window.open('#/cust/masterData/' + strids);
           }


           $scope.Del = function () {
               var entity = $scope.gridApi.selection.getSelectedRows()[0];
               entity.removeFlg = 0;
               entity.$update(function (res) {
                   alert(res);
                   $scope.searchCustomer();
               }, function (res) {
                   alert(res);
               });
           }

           $scope.exportCust = function () {
               var num = $scope.custCode;
               var name = $scope.custName;
               var status = $scope.status;
               var group = $scope.groupName;
               var collector = $scope.collector;
               var isic = $scope.isic;
               var begintime = $scope.startTime;
               var endtime = $scope.endTime;
               var miscollector = $scope.misCollect;
               var misgroup = $scope.misGroup;
               var billcode = $scope.billCode;
               var country = $scope.country;
               customerProxy.exportByCondition(num, name, status, group, collector, isic, begintime, endtime,
                                               miscollector, misgroup, billcode, country, function (path) {
                                                   window.location = path;
                                                   alert("Export Successful!");
                                               },
                                               function (res) { alert(res); });
           }

            $scope.importCust = function () {
                    var modalDefaults = {
                        templateUrl: 'app/common/upload/import.tpl.html',
                        controller: 'importCtrl',
                        windowClass: 'modalDialog'
                    };
                    modalService.showModal(modalDefaults, {}).then(function (result) {
                    });
                };

       }]);




